package com.huaguoguo.cyg.converter;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Converter;
import cn.hutool.core.util.ObjectUtil;
import com.huaguoguo.cyg.entity.weibo.dto.WbDetailDTO;
import com.huaguoguo.cyg.entity.weibo.dto.WbMediaDTO;
import com.huaguoguo.cyg.entity.weibo.to.WBDetail;
import com.huaguoguo.cyg.entity.weibo.to.WBMediaWrap;
import com.huaguoguo.cyg.enums.MediaTypeEnum;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 类说明： <br/>
 * author:huaguoguo
 * Date: 2020/2/11
 * Time: 13:47
 */
@Slf4j
public class WBDTOConverter {


    /**
     * 转换为指定类型<br>
     * 如果类型无法确定，将读取默认值的类型做为目标类型
     *
     * @param value        原始值
     * @param defaultValue 默认值
     * @return 转换后的值
     * @throws IllegalArgumentException 无法确定目标类型，且默认值为{@code null}，无法确定类型
     */
    public <T> T convert(Object value, T defaultValue) throws IllegalArgumentException {

        return null;
    }

    /**
     * WBDetail -> WbDetailDTO 转化函数
     *
     * @return
     */
    public static Function<WBDetail, WbDetailDTO> wbDetailDTOconvert() {
        return (t) -> {
            WbDetailDTO.WbDetailDTOBuilder builder = WbDetailDTO.builder();
            if (t.getWbFrom() != null) {
                builder.fromDateTime(t.getWbFrom().getFromDateTime())
                        .fromEndPoint(t.getWbFrom().getFromEndPoint());
            }
            if (t.getWbFeedExpand() != null) {
                builder.feedExpandMid(t.getWbFeedExpand().getMid());
            }
            if (t.getWbHandle() != null) {
                builder.forward(t.getWbHandle().getForward())
                        .commentNumber(t.getWbHandle().getComment())
                        .likeNumber(t.getWbHandle().getLike());
            }
            WbDetailDTO r = builder.build();
            BeanUtil.copyProperties(t, r);
            return r;
        };
    }

    public static Function<WBDetail, List<WbMediaDTO>> wbMediaDTOconvert() {
        return (t) -> {
            WBMediaWrap wbMediaWrap = t.getWbMediaWrap();
            if (wbMediaWrap == null) {
                // 判断转发内容有无多媒体内容
                if (t.getWbFeedExpand() != null && t.getWbFeedExpand().getWbMediaWrap() != null) {
                    t = t.getWbFeedExpand();
                    wbMediaWrap = t.getWbMediaWrap();
                } else {
                    log.debug("{}无多媒体内容", t.getMid());
                    return new ArrayList<>();
                }
            }
            WBDetail finalT = t;
            List<WbMediaDTO> wbMediaDTOList = null;
            if (CollUtil.isNotEmpty(wbMediaWrap.getClearPicSrcList())) {
                wbMediaDTOList = wbMediaWrap.getClearPicSrcList().stream()
                        .map(wbPic -> WbMediaDTO.builder().mid(finalT.getMid()).mediaType(MediaTypeEnum.CLEAR_PIC.getCode())
                                .mediaSrc(wbPic.getPicSrc()).build()
                        ).collect(Collectors.toList());
            }

            if (CollUtil.isNotEmpty(wbMediaWrap.getThumbPicSrcList())) {
                List<WbMediaDTO> tempList = wbMediaWrap.getThumbPicSrcList().stream()
                        .map(wbPic -> WbMediaDTO.builder().mid(finalT.getMid()).mediaType(MediaTypeEnum.THUMB_PIC.getCode())
                                .mediaSrc(wbPic.getPicSrc()).build()
                        ).collect(Collectors.toList());
                wbMediaDTOList.addAll(tempList);
            }
            return wbMediaDTOList;
        };
    }

}
